import pandas as pd

df1 = pd.DataFrame({"key1": [1, 2], "A": ["a", "b"]})
df2 = pd.DataFrame({"key2": [2, 3], "B": ["x", "y"]})

# 错误示例：直接合并会报错
# pd.merge(df1, df2)  # ValueError: No common columns to perform merge on.

# 正确做法：显式指定连接键
result = pd.merge(df1, df2, left_on="key1", right_on="key2")
print(result)

print('-'*50)

import pandas as pd

# 两个 DataFrame 有多个相同列名
df1 = pd.DataFrame({"key1": [1, 3], "key2": [2, 5], "A": ["a", "b"]})
df2 = pd.DataFrame({"key1": [1, 4], "key2": [2, 6], "B": ["x", "y"]})

# 自动使用所有同名列（key1 和 key2）作为连接键
result = pd.merge(df1, df2)
print(result)

print('-'*50)

import pandas as pd

# 两个 DataFrame 有多个相同列名
df1 = pd.DataFrame({"key1": [1, 3], "key3": [2, 5], "A": ["a", "b"]})
df2 = pd.DataFrame({"key1": [1, 4], "key2": [2, 6], "B": ["x", "y"]})

# 自动使用所有同名列（key1 和 key2）作为连接键
result = pd.merge(df1, df2)
print(result)

print('-'*50)

import pandas as pd

# 两个 DataFrame 有多个相同列名
df1 = pd.DataFrame({"key1": [1, 1,3], "key3": [2, 5,6], "A": ["a", "b","w"]})
df2 = pd.DataFrame({"key1": [1, 1,6], "key2": [2, 6,7], "B": ["x", "y","h"]})

# 自动使用所有同名列（key1 和 key2）作为连接键
result = pd.merge(df1, df2)
print(result)